package my.Views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import my.ai.AIEightQueen;
import my.eightZOne.R;
import my.eightZOne.explainActivity;

public class playView extends View {

    //当前触控坐标
    private int PositionX;
    private int PositionY;
    private boolean PickChess = false;

    //刀锋效果begin
    public static final int MESSAGE_CLIP = 100;
    public static final int REFRESH = 20;
    private Paint mPaint;
    private Path mPath;
    private int pointCount = 10;
    private int index = 0;
    private float offset = 20;
    private ArrayList<PointF> eventPointList = new ArrayList<PointF>();
    private ArrayList<PointF> pointPath = new ArrayList<PointF>();
    private CornerPathEffect mCornerPathEffect = new CornerPathEffect(10);
    //刀锋效果end

    //计时与计分begin
    private int grade = 30;                            //定义玩家得分
    private int time = 300;                            //时间
    private int DeadScore = 0;                        //定义玩家最高成绩
    private int DeadGrade = 30;                        //定义复盘成绩
    private Timer oneSecond;                        //计时器
    private TimerTask task;
    private int DeadCount;                            //定义死亡次数
    //计时与计分end

    private Canvas canvasOut;                        //定义画布
    private final Paint paint = new Paint();        //定义画笔

    //棋盘格点状态： 1--原始布局   2--放置棋子  0--未放棋子
    public int[][] ChessBoard = new int[10][10];

    AIEightQueen myai = new AIEightQueen();            //定义智能判别算法类
    private int Cflag = 0;                            //调试信息查看开关

    //特效声音
    MediaPlayer ok = MediaPlayer.create(this.getContext(), R.raw.flash);            //正确音效
    MediaPlayer wrong = MediaPlayer.create(this.getContext(), R.raw.wrong);        //错误音效
    MediaPlayer gulu = MediaPlayer.create(this.getContext(), R.raw.gulu);            //限制音效
    MediaPlayer dead = MediaPlayer.create(this.getContext(), R.raw.dead);            //死亡音效
    //MediaPlayer StartMusic=MediaPlayer.create(this.getContext(), R.raw.start);	//开始音乐
    MediaPlayer Succeed = MediaPlayer.create(this.getContext(), R.raw.succeed);    //过关音效

    //关卡背景音乐
    MediaPlayer MusicScene[] = new MediaPlayer[6];            //六个不同场景音乐
    private int MSceneFlag = -1;                                //标记上一个音乐场景

    private float Sheight;        //屏幕宽度
    private float Swidth;        //屏幕高度
    private float HeightScale;        //屏幕宽度与预设比例
    private float WidthScale;        //屏幕高度与预设比例
    private int LeftBorder;            //左边界
    private int BottomBorder;        //下边界
    private int CellW, CellH;        //单元格宽高

    private int[] Bar = {2, 3, 4, 5, 6, 8};    //关卡对应不同皇后难度
    private int barOrder = 0;            //关卡数
    private int Queen;                    //剩余皇后数
    private boolean BeginFlag = true;    //关卡开始标识
    private int ChessStyle = 1;

    private int flag_buttom = 0;        //被点击的按钮标识
    private int widthButtom;        //主按钮宽
    private int heightButtom;        //主按钮高

    //屏障布局样式引入
    Bitmap barrier1 = BitmapFactory.decodeResource(getResources(), R.drawable.barrier1);
    Bitmap barrier2 = BitmapFactory.decodeResource(getResources(), R.drawable.barrier2);
    Bitmap barrier3 = BitmapFactory.decodeResource(getResources(), R.drawable.barrier3);
    Bitmap barrier4 = BitmapFactory.decodeResource(getResources(), R.drawable.barrier4);
    Bitmap barrier5 = BitmapFactory.decodeResource(getResources(), R.drawable.barrier5);
    Bitmap barrier6 = BitmapFactory.decodeResource(getResources(), R.drawable.barrier6);

    //棋子样式图片引入
    Bitmap chessboard = BitmapFactory.decodeResource(getResources(), R.drawable.myzone);
    Bitmap chess0 = BitmapFactory.decodeResource(getResources(), R.drawable.chess);
    Bitmap chess1 = BitmapFactory.decodeResource(getResources(), R.drawable.chinaz1);
    Bitmap chess2 = BitmapFactory.decodeResource(getResources(), R.drawable.chinaz2);
    Bitmap chess3 = BitmapFactory.decodeResource(getResources(), R.drawable.chinaz3);
    Bitmap chess4 = BitmapFactory.decodeResource(getResources(), R.drawable.chinaz4);
    Bitmap chess5 = BitmapFactory.decodeResource(getResources(), R.drawable.chinaz5);
    Bitmap chess6 = BitmapFactory.decodeResource(getResources(), R.drawable.chinaz6);
    Bitmap chessS = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/MyZone/mypicture.jpg");            //引入自定义棋子

    //按钮图片引入
    Bitmap fpDown = BitmapFactory.decodeResource(getResources(), R.drawable.fpdown);
    Bitmap fpUp = BitmapFactory.decodeResource(getResources(), R.drawable.fpup);
    Bitmap ghUp = BitmapFactory.decodeResource(getResources(), R.drawable.ghup);
    Bitmap ghDown = BitmapFactory.decodeResource(getResources(), R.drawable.ghdown);
    Bitmap smDown = BitmapFactory.decodeResource(getResources(), R.drawable.smdown);
    Bitmap smUp = BitmapFactory.decodeResource(getResources(), R.drawable.smup);
    Bitmap tsUp = BitmapFactory.decodeResource(getResources(), R.drawable.tsup);
    Bitmap tsDown = BitmapFactory.decodeResource(getResources(), R.drawable.tsdown);
    Bitmap tcDown = BitmapFactory.decodeResource(getResources(), R.drawable.tcdown);
    Bitmap tcUp = BitmapFactory.decodeResource(getResources(), R.drawable.tcup);

    //小皇冠进度条图片
    Bitmap queenPic = BitmapFactory.decodeResource(getResources(), R.drawable.queen);

    //关卡名称图片
    Bitmap Bar1 = BitmapFactory.decodeResource(getResources(), R.drawable.bar1);
    Bitmap Bar2 = BitmapFactory.decodeResource(getResources(), R.drawable.bar2);
    Bitmap Bar3 = BitmapFactory.decodeResource(getResources(), R.drawable.bar3);
    Bitmap Bar4 = BitmapFactory.decodeResource(getResources(), R.drawable.bar4);
    Bitmap Bar5 = BitmapFactory.decodeResource(getResources(), R.drawable.bar5);
    Bitmap Bar6 = BitmapFactory.decodeResource(getResources(), R.drawable.bar6);

    Bitmap Bn1 = BitmapFactory.decodeResource(getResources(), R.drawable.bn1);
    Bitmap Bn2 = BitmapFactory.decodeResource(getResources(), R.drawable.bn2);
    Bitmap Bn3 = BitmapFactory.decodeResource(getResources(), R.drawable.bn3);
    Bitmap Bn4 = BitmapFactory.decodeResource(getResources(), R.drawable.bn4);
    Bitmap Bn5 = BitmapFactory.decodeResource(getResources(), R.drawable.bn5);
    Bitmap Bn6 = BitmapFactory.decodeResource(getResources(), R.drawable.bn6);

    Bitmap fpDownF, fpUpF;        //缩放后图片
    Bitmap smUpF, smDownF;
    Bitmap tsUpF, tsDownF;
    Bitmap tcUpF, tcDownF;
    Bitmap ghUpF, ghDownF;

    Bitmap Bar1F, Bar2F, Bar3F, Bar4F, Bar5F, Bar6F;
    Bitmap Bn1F, Bn2F, Bn3F, Bn4F, Bn5F, Bn6F, queenPicF;

    Bitmap chessboardF, chess0F, chess1F, chess2F, chess3F, chess4F, chess5F, chess6F, chessSF;
    Bitmap barrier1F, barrier2F, barrier3F, barrier4F, barrier5F, barrier6F;

    //重写父类构造函数
    public playView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);        //设置焦点调用系统监听
        myai.dfs(1);            //生成正确组合图
        init();                    //刀锋效果初始化
        //StartMusic.start();		//播放开始音乐

        MusicInit();            //音乐引入与初始化


        TimeCount();            //开始计时

        Screen();                //获取屏幕相关参数
        PictureScale();            //所有图片按屏幕大小比例缩放

        // shelview.getScore(10);
        // TODO Auto-generated constructor stub
    }

    //音乐引入与初始化
    protected void MusicInit() {
        MusicScene[0] = MediaPlayer.create(this.getContext(), R.raw.scene1);
        MusicScene[1] = MediaPlayer.create(this.getContext(), R.raw.scene2);
        MusicScene[2] = MediaPlayer.create(this.getContext(), R.raw.scene3);
        MusicScene[3] = MediaPlayer.create(this.getContext(), R.raw.scene4);
        MusicScene[4] = MediaPlayer.create(this.getContext(), R.raw.scene5);
        MusicScene[5] = MediaPlayer.create(this.getContext(), R.raw.scene6);
        for (int i = 0; i < 6; i++)
            MusicScene[i].setLooping(true);        //设置循环播放
    }

    //计时函数
    protected void TimeCount() {
        oneSecond = new Timer();
        task = new TimerTask() {
            public void run() {
                //时间大于0时才--
                if (time > 0)
                    time--;
                postInvalidate();                                    //再次调用onDraw函数重绘画布
            }
        };
        oneSecond.schedule(task, 1000, 1000);                        //1秒执行一次task
    }

    //获取屏幕相关参数
    protected void Screen() {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        Swidth = (float) dm.widthPixels;
        Sheight = (float) dm.heightPixels;
        WidthScale = Swidth / 480;        //屏幕宽度与预设比例
        HeightScale = Sheight / 800;        //屏幕高度与预设比例

        LeftBorder = (int) (Swidth * 0.1375);        //左边界
        BottomBorder = (int) (Sheight * 0.27);    //下边界
        CellH = (int) (45 * HeightScale);        //单元格高
        CellW = (int) (45 * WidthScale);        //单元格宽
    }

    //所有图片按屏幕大小比例缩放
    protected void PictureScale() {
        //小皇冠缩放
        queenPicF = scaleImg(queenPic, WidthScale, HeightScale);
        //屏障布局样式缩放
        barrier1F = scaleImg(barrier1, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        barrier2F = scaleImg(barrier2, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        barrier3F = scaleImg(barrier3, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        barrier4F = scaleImg(barrier4, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        barrier5F = scaleImg(barrier5, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        barrier6F = scaleImg(barrier6, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        //棋子样式缩放

        int bgWidth = chessboard.getWidth();
        int bgHeight = chessboard.getHeight();
        chessboardF = scaleImg(chessboard, WidthScale * 480 / bgWidth, HeightScale * 800 / bgHeight);

//        chessboardF = scaleImg(chessboard, WidthScale, HeightScale);
        chess0F = scaleImg(chess0, WidthScale, HeightScale);
        chess1F = scaleImg(chess1, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        chess2F = scaleImg(chess2, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        chess3F = scaleImg(chess3, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        chess4F = scaleImg(chess4, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        chess5F = scaleImg(chess5, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        chess6F = scaleImg(chess6, (float) (WidthScale * 1.5), (float) (HeightScale * 1.5));
        if (chessS != null) chessSF = scaleSelf(chessS);        //自定义缩放
        //按钮缩放
        fpDownF = scaleImg(fpDown, WidthScale, HeightScale);
        fpUpF = scaleImg(fpUp, WidthScale, HeightScale);

        smUpF = scaleImg(smUp, WidthScale, HeightScale);
        smDownF = scaleImg(smDown, WidthScale, HeightScale);

        tsUpF = scaleImg(tsUp, WidthScale, HeightScale);
        tsDownF = scaleImg(tsDown, WidthScale, HeightScale);

        tcUpF = scaleImg(tcUp, WidthScale, HeightScale);
        tcDownF = scaleImg(tcDown, WidthScale, HeightScale);

        ghUpF = scaleImg(ghUp, WidthScale, HeightScale);
        ghDownF = scaleImg(ghDown, WidthScale, HeightScale);

        Bar1F = scaleImg(Bar1, WidthScale, HeightScale);
        Bar2F = scaleImg(Bar2, WidthScale, HeightScale);
        Bar3F = scaleImg(Bar3, WidthScale, HeightScale);
        Bar4F = scaleImg(Bar4, WidthScale, HeightScale);
        Bar5F = scaleImg(Bar5, WidthScale, HeightScale);
        Bar6F = scaleImg(Bar6, WidthScale, HeightScale);

        Bn1F = scaleImg(Bn1, WidthScale, HeightScale);
        Bn2F = scaleImg(Bn2, WidthScale, HeightScale);
        Bn3F = scaleImg(Bn3, WidthScale, HeightScale);
        Bn4F = scaleImg(Bn4, WidthScale, HeightScale);
        Bn5F = scaleImg(Bn5, WidthScale, HeightScale);
        Bn6F = scaleImg(Bn6, WidthScale, HeightScale);

        //获得缩放后按主按钮长宽
        widthButtom = tcUpF.getWidth();
        heightButtom = tcUpF.getHeight();
    }

    //重写父类绘图函数
    @Override
    protected void onDraw(Canvas canvas) {
        canvasOut = canvas;
        canvasOut.drawBitmap(chessboardF, 0, 0, paint);        //绘制棋盘背景
        if (BeginFlag)        //关卡初始布局
            BarBegin();
        drawTitle();            //绘制标题
        drawChess();            //重绘棋盘
        drawRestQueen();        //绘制剩余皇后
        drawButton();        //绘制按钮

        drawTimer();         //绘制时间
        drawGrade();         //绘制积分

        CheckUp(Cflag);        //调试信息查看

        drawLine(canvas);        //绘制刀锋效果
        if (PickChess)            //绘制拖动棋子
            canvasOut.drawBitmap(CHStyle(), PositionX - CellW, PositionY - 2 * CellH, paint);
        super.onDraw(canvas);
    }

    //LH******************************************************
    //绘制成绩部分
    private void drawGrade() {
        // TODO Auto-generated method stub
        Bitmap num[] = new Bitmap[11];        //引入分数样式0-9图片
        num[0] = BitmapFactory.decodeResource(getResources(), R.drawable.score0);
        num[1] = BitmapFactory.decodeResource(getResources(), R.drawable.score1);
        num[2] = BitmapFactory.decodeResource(getResources(), R.drawable.score2);
        num[3] = BitmapFactory.decodeResource(getResources(), R.drawable.score3);
        num[4] = BitmapFactory.decodeResource(getResources(), R.drawable.score4);
        num[5] = BitmapFactory.decodeResource(getResources(), R.drawable.score5);
        num[6] = BitmapFactory.decodeResource(getResources(), R.drawable.score6);
        num[7] = BitmapFactory.decodeResource(getResources(), R.drawable.score7);
        num[8] = BitmapFactory.decodeResource(getResources(), R.drawable.score8);
        num[9] = BitmapFactory.decodeResource(getResources(), R.drawable.score9);
        num[10] = BitmapFactory.decodeResource(getResources(), R.drawable.score);

        Bitmap numF[] = new Bitmap[11];        //缩放后图片
        for (int i = 0; i < 11; i++) {
            numF[i] = scaleImg(num[i], (float) (WidthScale), (float) (HeightScale));        //分数样式0-9图片缩放
        }


        int ge, shi, bai;        //个十百位数字
        if (grade <= 0)        //防止负数
            ge = shi = bai = 0;
        else {
            ge = grade % 10;
            shi = (grade / 10) % 10;
            bai = grade / 100;
        }

        canvasOut.drawBitmap(numF[bai], Swidth * 80 / 480, Sheight * 120 / 800, paint);
        canvasOut.drawBitmap(numF[shi], Swidth * 80 / 480 + Swidth * 20 / 480, Sheight * 120 / 800, paint);
        canvasOut.drawBitmap(numF[ge], Swidth * 80 / 480 + 2 * Swidth * 20 / 480, Sheight * 120 / 800, paint);

        canvasOut.drawBitmap(numF[10], 18 * Swidth / 480, 120 * Sheight / 800, paint);
    }

    //绘制计时部分
    private void drawTimer() {
        // TODO Auto-generated method stub
        Bitmap num[] = new Bitmap[11];        //引入分数样式0-9图片
        num[0] = BitmapFactory.decodeResource(getResources(), R.drawable.score0);
        num[1] = BitmapFactory.decodeResource(getResources(), R.drawable.score1);
        num[2] = BitmapFactory.decodeResource(getResources(), R.drawable.score2);
        num[3] = BitmapFactory.decodeResource(getResources(), R.drawable.score3);
        num[4] = BitmapFactory.decodeResource(getResources(), R.drawable.score4);
        num[5] = BitmapFactory.decodeResource(getResources(), R.drawable.score5);
        num[6] = BitmapFactory.decodeResource(getResources(), R.drawable.score6);
        num[7] = BitmapFactory.decodeResource(getResources(), R.drawable.score7);
        num[8] = BitmapFactory.decodeResource(getResources(), R.drawable.score8);
        num[9] = BitmapFactory.decodeResource(getResources(), R.drawable.score9);
        num[10] = BitmapFactory.decodeResource(getResources(), R.drawable.times);

        Bitmap numF[] = new Bitmap[11];        //缩放后图片
        for (int i = 0; i < 11; i++) {
            numF[i] = scaleImg(num[i], (float) (WidthScale), (float) (HeightScale));        //分数样式0-9图片缩放
        }


        int ge, shi, bai;        //个十百位数字
        ge = time % 10;
        shi = (time / 10) % 10;
        bai = time / 100;

        canvasOut.drawBitmap(numF[bai], Swidth * 360 / 480, Sheight * 120 / 800, paint);
        canvasOut.drawBitmap(numF[shi], Swidth * 360 / 480 + Swidth * 20 / 480, Sheight * 120 / 800, paint);
        canvasOut.drawBitmap(numF[ge], Swidth * 360 / 480 + 2 * Swidth * 20 / 480, Sheight * 120 / 800, paint);

        canvasOut.drawBitmap(numF[10], 440 * Swidth / 480, 120 * Sheight / 800, paint);

    }
    //LH*****************************************************

    //调试信息查看
    private void CheckUp(int Cflag) {
        // TODO Auto-generated method stub
        if (2 == Cflag) {
            //canvasOut.drawText("flag_buttom="+flag_buttom,10,90,paint);//按钮选择查看
            canvasOut.drawText("cell size : " + CellH + "*" + CellW + " screen : " + Sheight + "*" + Swidth, 60 * WidthScale, 200 * HeightScale, paint);
            //当前棋局答案
            canvasOut.drawText("barOrder = " + barOrder + "Bar = " + Bar[barOrder], 10, 70, paint);
            canvasOut.drawText("Queen = " + Queen, 10, 80, paint);
            canvasOut.drawText("bar:" + myai.bar[0] + myai.bar[1] + myai.bar[2] + myai.bar[3] + myai.bar[4] + myai.bar[5] + myai.bar[6] + myai.bar[7], 10, 90, paint);
            int szc = myai.match(ChessBoard);
            if (szc != -1)        //当有匹配棋局时输出
            {
                int match[] = new int[8];
                for (int j = 0; j < 8; j++)
                    match[j] = 9 - myai.loca[szc][j];
                canvasOut.drawText("match:" + match[0] + match[1] + match[2] + match[3] + match[4] + match[5] + match[6] + match[7], 10, 110, paint);
            }
        }
    }

    //绘制按钮
    private void drawButton() {
        // TODO Auto-generated method stub
        //绘制静态按钮
        canvasOut.drawBitmap(fpUpF, Swidth * 38 / 480, Sheight * 715 / 800, paint);
        canvasOut.drawBitmap(ghUpF, Swidth * 119 / 480, Sheight * 715 / 800, paint);
        canvasOut.drawBitmap(smUpF, Swidth * 200 / 480, Sheight * 715 / 800, paint);
        canvasOut.drawBitmap(tsUpF, Swidth * 284 / 480, Sheight * 715 / 800, paint);
        canvasOut.drawBitmap(tcUpF, Swidth * 366 / 480, Sheight * 715 / 800, paint);
        //绘制动态按钮
        switch (flag_buttom) {
            case 1:
                canvasOut.drawBitmap(fpDownF, Swidth * 38 / 480, Sheight * 715 / 800, paint);
                break;
            case 3:
                canvasOut.drawBitmap(ghDownF, Swidth * 119 / 480, Sheight * 715 / 800, paint);
                break;
            case 5:
                canvasOut.drawBitmap(smDownF, Swidth * 200 / 480, Sheight * 715 / 800, paint);
                break;
            case 7:
                canvasOut.drawBitmap(tsDownF, Swidth * 284 / 480, Sheight * 715 / 800, paint);
                break;
            case 9:
                canvasOut.drawBitmap(tcDownF, Swidth * 366 / 480, Sheight * 715 / 800, paint);
                break;
            case 0:
                break;
        }
    }

    //关卡初始布局
    private void BarBegin() {
        if (MSceneFlag != -1 && MSceneFlag != barOrder)
            MusicScene[MSceneFlag].stop();        //上一音乐停止
        MusicScene[barOrder].start();            //新音乐开始
        MSceneFlag = barOrder;    //音乐场景跟踪标记

        BeginFlag = false;        //棋局开始标识为假
        Queen = Bar[barOrder];    //剩余皇后数
        myai.BarProduce(Bar[barOrder]);        //产生棋局
        //清空当前棋局
        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++)
                ChessBoard[i][j] = 0;
        //按产生棋局布局
        for (int i = 0; i < 8; i++) {
            if (myai.bar[i] > -1)
                ChessBoard[i][myai.bar[i] - 1] = 1;
        }
    }

    //绘制棋子
    private void drawChess() {

        //扫描数组并画棋子
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (2 == ChessBoard[i][j]) {
                    canvasOut.drawBitmap(CHStyle(), LeftBorder - 10 * HeightScale + i * CellW, BottomBorder - 10 * HeightScale + j * CellH, paint);
                } else if (1 == ChessBoard[i][j]) {
                    canvasOut.drawBitmap(BRStyle(), LeftBorder - 10 * HeightScale + i * CellW, BottomBorder - 10 * HeightScale + j * CellH, paint);
                }
            }
        }
    }

    //棋子样式选择
    private Bitmap CHStyle() {
        switch (ChessStyle) {
            case 1:
                if (chess1 != null)
                    return chess1F;
            case 2:
                return chess2F;
            case 3:
                return chess3F;
            case 4:
                return chess4F;
            case 5:
                return chess5F;
            case 6:
                return chess6F;
        }
        return chess0F;
    }

    //屏障样式选择
    private Bitmap BRStyle() {
        switch (barOrder) {
            case 0:
                return barrier1F;
            case 1:
                return barrier2F;
            case 2:
                return barrier3F;
            case 3:
                return barrier4F;
            case 4:
                return barrier5F;
            case 5:
                return barrier6F;
        }
        return chess0F;
    }

    //绘制剩余皇后
    private void drawRestQueen() {
        int rest;
        if (PickChess)
            rest = Queen - 1;
        else
            rest = Queen;
        //小皇冠queenPicF
        for (int i = 0; i < rest; i++)
            canvasOut.drawBitmap(CHStyle(), (float) (40 * WidthScale + 51.4 * i * WidthScale), 625 * HeightScale, paint);
    }

    //绘制关卡标题与名称
    private void drawTitle() {
        switch (barOrder)    //根据当前关卡数选择图片
        {
            case 0:
                canvasOut.drawBitmap(Bn1F, 35 * HeightScale, 35 * HeightScale, paint);
                canvasOut.drawBitmap(Bar1F, 190 * HeightScale, 120 * HeightScale, paint);
                break;
            case 1:
                canvasOut.drawBitmap(Bn2F, 35 * HeightScale, 35 * HeightScale, paint);
                canvasOut.drawBitmap(Bar2F, 190 * HeightScale, 120 * HeightScale, paint);
                break;
            case 2:
                canvasOut.drawBitmap(Bn3F, 35 * HeightScale, 35 * HeightScale, paint);
                canvasOut.drawBitmap(Bar3F, 190 * HeightScale, 120 * HeightScale, paint);
                break;
            case 3:
                canvasOut.drawBitmap(Bn4F, 35 * HeightScale, 35 * HeightScale, paint);
                canvasOut.drawBitmap(Bar4F, 190 * HeightScale, 120 * HeightScale, paint);
                break;
            case 4:
                canvasOut.drawBitmap(Bn5F, 35 * HeightScale, 35 * HeightScale, paint);
                canvasOut.drawBitmap(Bar5F, 190 * HeightScale, 120 * HeightScale, paint);
                break;
            case 5:
                canvasOut.drawBitmap(Bn6F, 35 * HeightScale, 35 * HeightScale, paint);
                canvasOut.drawBitmap(Bar6F, 190 * HeightScale, 120 * HeightScale, paint);
                break;
        }
    }

    //重写触屏事件函数
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        float x = event.getX(0);        //获取横坐标
        float y = event.getY(0);        //获取纵坐标
        PositionX = (int) x;        //记录x坐标
        PositionY = (int) y;        //记录y坐标
        int xx = (int) ((PositionX - CellW / 2 - LeftBorder) / CellW);                //横坐标拖动横格数
        int yy = (int) ((PositionY - CellH * 1.5 - BottomBorder) / CellH);            //纵坐标拖动纵格数
        int xxd = (int) ((PositionX - LeftBorder) / CellW);            //横坐标点击横格数
        int yyd = (int) ((PositionY - BottomBorder) / CellH);        //纵坐标点击纵格数

        //主棋盘区域
        if ((0 <= xx) && (xx < 8) && (0 <= yy) && (yy < 8) && (PickChess)) {    //判断触点是否在棋盘区域内
            //FlashConflict(1,1,2,2);
            if (myai.bar[xx] - 1 == yy)    //判断棋子是否为初始布局
            {
                gulu.start();            //播放限制音效
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                if (myai.valid(xx, yy, ChessBoard)) {
                    ok.start();                        //播放棋子音效
                    if (0 == ChessBoard[xx][yy])        //触击屏幕时相应棋子状态转换
                    {
                        ChessBoard[xx][yy] = 2;
                        Queen--;
                        grade += 30;
                    } else {
                        ChessBoard[xx][yy] = 0;
                        Queen++;
                        grade -= 30;
                    }
                } else {
                    DeadCount++;                //死亡次数+1
                    try {
                        myai.order(DeadScore);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }        //成绩计入排行榜
                    DeadScore = 0;
                    grade -= 10;
                    time -= 30;
                    if (grade < 0 || time <= 0) {
                        barOrder = 0;    //返回第一关
                        dead.start();    //播放死亡音效
                        time = 300;        //重新计时
                        grade = 30;
                        BarBegin();        //重新布局
                    }
                    wrong.start();        //播放错误音效
                }
            }
        }

        //取消放置的棋子
        if ((0 <= xxd && xxd < 8 && 0 <= yyd && yyd < 8) && (event.getAction() == MotionEvent.ACTION_DOWN) && (ChessBoard[xxd][yyd] != 0) && (myai.bar[xxd] - 1 != yyd)) {
            ChessBoard[xxd][yyd] = 0;
            Queen++;
            grade -= 30;
        }

        //按钮区域
        if ((x > Swidth * 38 / 480 && x < Swidth * 38 / 480 + widthButtom) && (y > Sheight * 715 / 800 && y < Sheight * 715 / 800 + heightButtom + 20)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {//复盘
                flag_buttom = 1;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 2;
                grade = DeadGrade;
                BarBegin();        //关卡重新开始
            }
        }
        if ((x > Swidth * 119 / 480 && x < Swidth * 119 / 480 + widthButtom) && (y > Sheight * 715 / 800 && y < Sheight * 715 / 800 + heightButtom + 20)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {//更换
                flag_buttom = 3;
                if (6 == ChessStyle)
                    ChessStyle = 1;
                else
                    ChessStyle++;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 4;
            }
        }

        if ((x > Swidth * 200 / 480 && x < Swidth * 200 / 480 + widthButtom) && (y > Sheight * 715 / 800 && y < Sheight * 715 / 800 + heightButtom + 20)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {//说明
                flag_buttom = 5;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 6;
                Intent intent = new Intent(this.getContext(), explainActivity.class);
                this.getContext().startActivity(intent);
            }
        }

        if ((x > Swidth * 284 / 480 && x < Swidth * 284 / 480 + widthButtom) && (y > Sheight * 715 / 800 && y < Sheight * 715 / 800 + heightButtom + 20)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {//提示
                flag_buttom = 7;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 8;
                pointOut();
            }
        }

        if ((x > Swidth * 366 / 480 && x < Swidth * 366 / 480 + widthButtom) && (y > 715 * HeightScale && y < 715 * HeightScale + heightButtom + 20)) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {//退出
                flag_buttom = 9;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                flag_buttom = 10;
                //System.exit(0);
                MusicScene[barOrder].stop();
                ((Activity) playView.this.getContext()).finish();
                //onKeyDown(KeyEvent.KEYCODE_BACK, null);
            }
        }

        if (x < 60 && y < 60 && event.getAction() == MotionEvent.ACTION_DOWN)            //调试信息查看开关
        {
            Cflag++;
            if (Cflag > 3)
                Cflag = 0;
        }

        //剩余棋子区域
        if (event.getAction() == MotionEvent.ACTION_DOWN && x < (40 * WidthScale + 51.4 * Queen * WidthScale) && y > 625 * HeightScale && y < Sheight * 700 / 800)
            PickChess = true;
        //当放满皇后则还原并进入下一关
        if ((0 == Queen) && (!BeginFlag)) {
            Succeed.start();        //播放胜利音乐
            BeginFlag = true;
            if (barOrder < 5)        //是否通关
                barOrder++;
            else {
                barOrder = 0;
            }

            //过关更换棋子样式
            if (6 == ChessStyle)
                ChessStyle = 1;
            else
                ChessStyle++;

            //成绩计入排行榜
            DeadGrade = grade;
            DeadScore = grade + time;
            //时间复原
            time = 300;
        }

        //刀锋效果 - begin
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                index = 0;
                myHandler.removeMessages(MESSAGE_CLIP);
                index++;
                eventPointList.clear();
                eventPointList.add(new PointF(x, y));
                break;
            case MotionEvent.ACTION_MOVE:

                PointF eventMove = new PointF(x, y);
                if (index > 0 && distance(eventMove, eventPointList.get(index - 1)) > 10.0f) {
                    if (index > pointCount) {
                        eventPointList.add(new PointF(x, y));
                        eventPointList.remove(0);
                    } else {
                        eventPointList.add(new PointF(x, y));
                        index++;
                    }

                } else {
                    if (index > 4) {
                        eventPointList.remove(0);
                        index--;
                    }
                }
                invalidate();

                break;
            case MotionEvent.ACTION_UP:
                PickChess = false;        //松开棋子
                myHandler.sendEmptyMessageDelayed(MESSAGE_CLIP, 100);
                break;
        }
        //刀锋效果 - end

        postInvalidate();        //再次调用onDraw函数重绘画布
        return true;
    }

    //提示函数，放上一颗正确棋子
    private void pointOut() {
        // TODO Auto-generated method stub
        Boolean StopFlag = true;
        int szc = myai.match(ChessBoard);
        if (szc != -1)        //当有匹配棋局时输出
        {
            for (int i = 0; i < 8; i++)
                if (StopFlag && (0 == ChessBoard[i][myai.loca[szc][i] - 1])) {
                    ChessBoard[i][myai.loca[szc][i] - 1] = 2;
                    Queen--;        //棋盘放上皇后，剩余皇后数减少
                    StopFlag = false;
                    //shelview.subScore(10);
                }
        }
    }

    //冲突闪烁函数
    public void flashConflict(int x1, int y1, int x2, int y2) {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 5; i++) {
            ChessBoard[y1][x1] = 2;
            ChessBoard[y2][x2] = 2; //冲突的两棋子在棋盘上
            postInvalidate();        //再次调用onDraw函数重绘画布
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            postInvalidate();        //再次调用onDraw函数重绘画布
            ChessBoard[y1][x1] = 0;
            ChessBoard[y2][x2] = 0; //冲突的两棋子不在棋盘上
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        ChessBoard[y2][x2] = 2;//保留前面的皇后
        postInvalidate();        //再次调用onDraw函数重绘画布
        //SetDlgItemInt(IDC_DEAD,++dead,0);	//死亡数加1
    }

    //缩放图片函数
    public Bitmap scaleImg(Bitmap bm, float scaW, float scaH) {
        // 获得图片的宽高		
        int width = bm.getWidth();
        int height = bm.getHeight();

        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaW, scaH);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    //自定义图片缩放函数
    public Bitmap scaleSelf(Bitmap bm) {
        // 获得图片的宽高		
        int width = bm.getWidth();
        int height = bm.getHeight();

        float scaW = 40 / width;
        float scaH = 50 / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaW, scaH);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    //刀锋效果 - 初始化
    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setPathEffect(mCornerPathEffect);
        mPaint.setColor(0xCCFFFF00);
        mPath = new Path();
    }

    //刀锋效果 - 画线
    private void drawLine(Canvas canvas) {

        if (index < 2) {
            return;
        } else {
            float w = offset / ((index < pointCount ? index : pointCount) + 5);
            PointF tempLast = eventPointList.get(index - 1);
            float k = getK(tempLast, eventPointList.get(index - 2));
            PointF lastPoint = new PointF();
            lastPoint.x = (float) (tempLast.x + Math.cos(k) * 10);
            lastPoint.y = (float) (tempLast.y + Math.sin(k) * 10);
            pointPath.add(lastPoint);
            for (int i = index - 2; i > 0; i--) {
                float kLeft = getK(eventPointList.get(i),
                        eventPointList.get(i - 1));
                PointF temp = new PointF();
                temp.x = (float) (eventPointList.get(i).x - Math.sin(kLeft) * w
                        * i);
                temp.y = (float) (eventPointList.get(i).y + Math.cos(kLeft) * w
                        * i);
                pointPath.add(0, temp);
            }
            pointPath.add(0, eventPointList.get(0));
            pointPath.add(lastPoint);
            for (int i = index - 2; i > 0; i--) {
                float kRight = getK(eventPointList.get(i),
                        eventPointList.get(i - 1));
                PointF temp = new PointF();
                temp.x = (float) (eventPointList.get(i).x + Math.sin(kRight) * w
                        * i);
                temp.y = (float) (eventPointList.get(i).y - Math.cos(kRight) * w
                        * i);
                pointPath.add(temp);
            }
            pointPath.add(eventPointList.get(0));
            drawLineByPoints(canvas);

        }
    }

    public float distance(PointF p1, PointF p2) {
        float tt = (float) Math.pow((p1.x - p2.x) * (p1.x - p2.x)
                + (p1.y - p2.y) * (p1.y - p2.y), 0.5);
        return tt;

    }

    public float getK(PointF p1, PointF p2) {
        return (float) Math.atan2(p1.y - p2.y, p1.x - p2.x);
    }

    public void drawLineByPoints(Canvas canvas) {
        int pathSize = index * 2;
        mPath.reset();
        mPath.moveTo(pointPath.get(0).x, pointPath.get(0).y);
        for (int i = 1; i < pathSize; i++) {
            mPath.lineTo(pointPath.get(i).x, pointPath.get(i).y);
        }
        //带填充色的path
        canvas.drawPath(mPath, mPaint);
        //不带填充色的path
        //canvas.drawPath(mPath, mStrokePaint);
        pointPath.clear();

    }

    private Handler myHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
                case MESSAGE_CLIP:
                    if (index > 1) {
                        eventPointList.remove(0);
                        index--;
                        this.sendEmptyMessageDelayed(MESSAGE_CLIP, REFRESH);
                    } else {
                        index = 0;
                        eventPointList.clear();
                        return;
                    }
                    invalidate();

                    break;
            }
        }
    };

    public void clear() {
        mPath.reset();
        mPath = null;
        eventPointList.clear();
        eventPointList = null;
        eventPointList = null;
        eventPointList = null;
        mCornerPathEffect = null;
        mPaint = null;
        myHandler = null;
    }
    //刀锋效果 - end
}

